package com.sihai.ehr.web.action.salary;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.AttendanceImportLog;
import com.sihai.ehr.service.SalaryAttendanceService;
import com.sihai.ehr.utils.EHRConfig;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import java.util.List;

/**
 * author: wxn
 * since: 13-3-11 Time: 下午12:01
 */
public class SalaryAttendanceAction extends EHRAction{
    @BeanInject
    private SalaryAttendanceService salaryAttendanceService;

    private Integer year;
    private Integer month;

    private AttendanceImportLog attendaceLog;
    private AttendanceImportLog erpLog;

    private LimitedList attendances;

    private Boolean result;

    private Long examCount;
    private Long employeeCount;
    private float examProgress;
    private float totalProgress;

    //@Action(value = "/ajax/attendance-import-log", results = {@Result(name = "success", type = "json", params = {"root", "logs"})})
    @Action(value = "/salary/attendance-import-log", results = {@Result(name = "success", location = "salary_balance_import.jsp")})
    public String findAttendanceLog(){
        try {
            List<AttendanceImportLog> logs = salaryAttendanceService.findAttendanceLog(organizationId, year, month);
            if(logs != null && logs.size() > 0){
                for(AttendanceImportLog log : logs){
                    if(StringUtils.equals(log.getType(), "attendance")){
                        attendaceLog = log;
                    }else{
                        erpLog = log;
                    }
                }
            }
            //统计考核总数
            /*if(month == 1 || month == 5 || month == 7 || month == 11){ //单月

            }else if(month == 2 || month == 4 || month == 8 || month == 10){ //单月， 双月

            }else if(month == 3 || month == 9){//单月， 季

            }else if(month == 6){//单月， 双月， 季， 半年

            }else if(month == 12){//单月， 双月， 季， 半年， 年

            }*/
            examCount = salaryAttendanceService.findExamCount(organizationId, year, month);
            if(attendaceLog == null){
                employeeCount = salaryAttendanceService.findEmployeeCount(organizationId);
            }else{
                employeeCount = (long)attendaceLog.getTotal();
            }
            examProgress = employeeCount == 0 ? 0 : (float)examCount/(float)employeeCount*100;
            totalProgress = examProgress;
            if(attendaceLog != null){
                totalProgress += attendaceLog.getProgress();
            }
            if(erpLog != null){
                totalProgress += erpLog.getProgress();
            }
            totalProgress = totalProgress/3;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    //清除导入的考勤数据
    @Action(value = "/ajax/attendance-data-remove", results = {@Result(name = "success", type = "json", params = {"root", "result"})})
    public String clean(){
        try{
            salaryAttendanceService.delete(organizationId, year, month, getParameter("type"));
            result = true;
        }catch (Exception e){
        }
        return SUCCESS;
    }


    //@Action(value = "/salary/attendance-data-list", results = {@Result(name = "success", location = "attendance_data_list.jsp")}) //老的考勤视图
    @Action(value = "/salary/attendance-data-list", results = {@Result(name = "success", location = "attendance_new_data_list.jsp")}) //新的考勤视图
    public String findAttendanceData(){
        attendances = salaryAttendanceService.findAttendances("attendance", organizationId, year, month, createLimit(), createSorter());
        setPageUrl("salary/attendance-data-list.xhtml");
        paging(attendances);
        return SUCCESS;
    }
    @Action(value = "/salary/erp-data-list", results = {@Result(name = "success", location = "attendance_erp_data_list.jsp")})
    public String finErpData(){
        attendances = salaryAttendanceService.findAttendances("erp", organizationId, year, month, createLimit(), createSorter());
        setPageUrl("salary/erp-data-list.xhtml");
        paging(attendances);
        return SUCCESS;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public Integer getYear() {
        return year;
    }

    public void setMonth(Integer month) {
        this.month = month;
    }

    public Integer getMonth() {
        return month;
    }

    public AttendanceImportLog getAttendaceLog() {
        return attendaceLog;
    }

    public AttendanceImportLog getErpLog() {
        return erpLog;
    }

    public LimitedList getAttendances() {
        return attendances;
    }

    public Boolean getResult() {
        return result;
    }

    public Long getExamCount() {
        return examCount;
    }

    public float getExamProgress() {
        return examProgress;
    }

    public Long getEmployeeCount() {
        return employeeCount;
    }

    public float getTotalProgress() {
        return totalProgress;
    }
    public String getTotalProgressStr(){
        return EHRConfig.mDecimalFormat.format(totalProgress);
    }
}
